% Copyright (c) 2012, John Gebbie. All rights reserved.
%
% This library is free software; you can redistribute it and/or modify it
% under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation; either version 2.1 of the License, or (at
% your option) any later version.
%
% This library is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
% General Public License for more details.
%
% You should have received a copy of the GNU Lesser General Public License
% along with this library; if not, write to the Free Software Foundation,
% Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA


function [ freqs,bins,binwidth,nyq ] = fftinfo(sampfreq,fftsize,nyqpos)
% [ freqs,bins,binwidth,nyq ] = fftinfo(sampfreq,fftsize,nyqpos)
%
%   Produce useful information about the output of the fft function.
%
% INPUTS
%       sampfreq        : sampling frequency (Hz)
%       fftsize         : size of vector *output* by fft. this is specified
%                           by the 2nd paramter to 'fft'.
%       nyqpos          : make the nyquist frequency positive for even
%                           numbered fftsizes. by default it is negative.
%
% OUTPUTS
%       freqs           : the frequency of each bin (1-by-fftsize)
%       bins            : the bin numbers (1-by-fftsize)
%       binwidth        : the width of each bin (scalar)
%       nyq             : the nyquest frequency (scalar)
%
% EXAMPLE
%
%     >> fftinfo(100,10)
% 
%     ans =
% 
%          0    10    20    30    40   -50   -40   -30   -20   -10


binwidth = sampfreq/fftsize;
bins=(mod(((0:fftsize-1)+floor(fftsize/2)), fftsize)-floor(fftsize/2));
nyq=abs(bins(floor(fftsize/2)+1));
if exist('nyqpos','var') && nyqpos && mod(fftsize,2)==0
    bins(nyq+1) = abs(bins(nyq+1));
end
freqs=binwidth.*bins;
